package com.ynkbny.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ynkbny.base.PageResult;
import com.ynkbny.domain.RouterLow;
import com.ynkbny.domain.User;
import com.ynkbny.domain.pojo.RoleWithRouter;
import com.ynkbny.service.impl.MenuServiceImpl;
import com.ynkbny.service.impl.RouterServiceImpl;
import com.ynkbny.util.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author zelin.wang
 * @description 菜单Controller
 * @date 9/4/2023
 */
@RestController
@Api(tags = "菜单权限管理接口")
@RequestMapping("/menu")
public class MenuController {
    @Resource
    MenuServiceImpl menuService;
    @Resource
    RouterServiceImpl routerService;

    @ApiOperation("菜单分页查询")
    @GetMapping("/pageList")
    public Res<PageResult<RouterLow>> pageList(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                               @RequestParam(name = "pageSize", required = false, defaultValue = "15") int pageSize,
                                               @RequestParam(name = "condition", required = false) String conditionJson) {
        return Res.ok(menuService.pageList(conditionJson, pageNum, pageSize));
    }

    @ApiOperation("菜单根据id删除")
    @DeleteMapping("/delById")
    public Res delById (int id){
        return menuService.removeById(id)?Res.ok("删除成功！"):Res.fail("删除失败！");
    }

    @ApiOperation("新增或更改")
    @PutMapping("/saveOrUpdate")
    public Res saveOrUpdate (@RequestBody RouterLow routerLow){
        return menuService.saveOrUpdate(routerLow)?Res.ok("上传成功！"):Res.fail("上传失败！");
    }

    @ApiOperation("角色权限绑定")
    @PostMapping("/roleBinding")
    public Res roleBinding (@RequestBody List<RoleWithRouter> ids){

        return Res.ok(menuService.roleBind(ids));
    }

    @ApiOperation("根据角色id查询绑定菜单")
    @GetMapping("queryByRoleId")
    public Res queryByRoleId(String id){
        return Res.ok(routerService.getRouterByRoleId(id));
    }
}
